/* **************************************************
 * User Group Manager
 *      author:     Baskin Tapkan
 *          written for Code Plex
 *              April - 2008
 *                  http://tapkan.com
 * ****************************************************
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace UserGroupManager
{
    public partial class NewUser : Form
    {
        string computerName;
        LocalUser lu;

        public NewUser(string systemName)
        {
            computerName = systemName;
            InitializeComponent();
            this.Text = "New User for " + computerName;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        // Load events
        private void NewUser_Load(object sender, EventArgs e)
        {
            chkMustChangePswd.Checked = true;
            chkCannotChangePswd.Enabled = false;
            chkPswdNeverExpires.Enabled = false;

            // toggle password
            if ((txtPassword.Text.Length > 0) &&
                (txtPassword.Text.Equals(txtConfPassword.Text)))
            {
                btnCreate.Enabled = true;
                btnCreate.Select();
            }
        }

        // TODO : user account - check for permittable characters, length etc.
        private void btnCreate_Click(object sender, EventArgs e)
        {
            // assuming the account information is correct, pass the credentials
            // to the underlying layer
            SetUserAccount();
            UserMgr um = new UserMgr();
            um.ComputerName = computerName;
            string result = um.CreateNewAccount(lu);
            InformUI(result);
        }

        private void SetUserAccount()
        {
            lu = new LocalUser();
            lu.UserName = txtUserName.Text;
            lu.UserFullName = txtFullName.Text;
            lu.UserDescription = txtDescription.Text;
            lu.UserPswd = txtPassword.Text;
            lu.AccountDisabled = chkAccountDisabled.Checked;
            lu.PasswordLastSet = chkMustChangePswd.Checked;
             //backwards due to wording - if the "User cannot Change password" is checked,
             //it means that "Password is not Changeable", set it to FALSE, and vices versa.
            lu.PasswordChangeable = !chkCannotChangePswd.Checked;
             //backwards again as above - if the "Password never expires" is checked,
             //it means that Password is NOT going to expire, thus it is set to FALSE, and vices versa
            lu.PasswordExpires = !chkPswdNeverExpires.Checked;
        }

        private void txtConfPassword_Leave(object sender, EventArgs e)
        {
            if (!(txtPassword.Text.Equals(txtConfPassword.Text)))
            {
                // flag password mismatch
                MessageBox.Show("Passwords do not match!  Please try again...", "Password mismatch",
                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                // reset txt fields
                txtPassword.Text = "";
                txtConfPassword.Text = "";
            }
            else
                btnCreate.Enabled = true;
        }

        private void InformUI(string result)
        {
            staUserMgr.Items[0].Text = result;
        }

        // handle mutually exclusiveness on these attributes
        private void chkMustChangePswd_CheckedChanged(object sender, EventArgs e)
        {
            if (chkMustChangePswd.Checked)
            {
                chkCannotChangePswd.Enabled = false;
                chkPswdNeverExpires.Enabled = false;
            }
            else
            {
                chkCannotChangePswd.Enabled = true;
                chkPswdNeverExpires.Enabled = true;
            }
        }

    }
}